package com.pms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.pms.service.LoadRepaymentDetail;
import com.pms.utils.MD5;
import com.pms.utils.PageFetcher;
import com.pms.utils.PropsUtil;

@Service
public class LoadRepaymentDetailImpl implements LoadRepaymentDetail {
	
	static Properties prop = PropsUtil.loadProps("other.properties");
	public static final String SERVICE_BASE_ACCOUNT = PropsUtil.getString(prop, "service.base.account");
	public static final String SERVICE_BASE_PASSWORD = PropsUtil.getString(prop, "service.base.password");
	public static final String REPAYMENT_DOWNLOAD_PATH = PropsUtil.getString(prop, "hxtFreePhone.repayment.download.path");
	public static Logger log = Logger.getLogger(LoadRepaymentDetailImpl.class);
	
	/**
	 * 获取还款明细数据
	 * 
	 */
	@Override
	public String loadRepaymentDetails(int stageNum) {
		String timeStamp = createTimeStamp();
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("reqNum",timeStamp);
		jsonObject.accumulate("appType","3");
		jsonObject.accumulate("userName",SERVICE_BASE_ACCOUNT);
		jsonObject.accumulate("timeStack",timeStamp);
		jsonObject.accumulate("checkCode",createCheckCode(timeStamp));
		jsonObject.accumulate("stageNum",stageNum);
		jsonObject.accumulate("businessNo","A001");
		jsonObject.accumulate("partnerId","A001");
		jsonObject.accumulate("transId","GYJFA011");
		log.info(jsonObject.toString());
		String result = null;
		log.info(REPAYMENT_DOWNLOAD_PATH);
		result = PageFetcher.sendPostMethod(REPAYMENT_DOWNLOAD_PATH, jsonObject.toString(), "utf-8");
		return result;
	}
	
	public static String createTimeStamp() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

	public static String createCheckCode(String timeStamp) {
		String str = "";
		str = MD5.GetMd5(SERVICE_BASE_ACCOUNT + timeStamp + SERVICE_BASE_PASSWORD);
		log.info(str.toUpperCase());
		return str.toUpperCase();
	}
	
	public static void main(String[] args) {
		LoadRepaymentDetailImpl load = new LoadRepaymentDetailImpl();
		System.out.println(load.loadRepaymentDetails(3));
	}

}
